<?php
/**
 * configuration object...
 * run time conf only, so naturally...
 * @static
 * @author bbqq
 * @copyright 2010 qstufie.com
 * @package com.sprpc.utility
 */
class SPRPC_Utility_Config
{
    /**
     * stop instantiating
     * @return return false
     */
    private function __construct()
    {
        return false;

    }// end __construct


    /**
     * environment...
     * @param str $env dev|test|uat|live
     * @return void
     */
    static public function env($env)
    {
        // upper...
        $env = strtoupper($env);
        SPRPC_Utility_Registry::register('__CONF_CURRENT_NS', $env);

    }// end env


    /**
     * get current environment
     * @return str
     */
    static public function getCurrentEnv()
    {
        return SPRPC_Utility_Registry::get('__CONF_CURRENT_NS');

    }// end getCurrentEnv


    /**
     * get current environment
     * @return str
     */
    static public function getParentEnv()
    {
        return SPRPC_Utility_Registry::get('__CONF_PARENT_NS');

    }// end getCurrentEnv


    /**
     * extend the namespace if exists...
     * @param str $env
     * @return void
     */
    static public function extend($env)
    {
        // upper...
        $env = strtoupper($env);

        // check if exists...
        $parentEnvConf = self::get(null, null, $env);
        if (!empty($parentEnvConf)) {
            SPRPC_Utility_Registry::register('__CONF_PARENT_NS', $env);
            // and set it like it should be!!
            SPRPC_Utility_Registry::register('__CONF_NS_' . self::getCurrentEnv(), $parentEnvConf);
        } else {
            throw new Exception('PARENT ENVIRONMENT [' . $env . '] IS EMPTY');
        }

    }// end extend


    /**
     * set config
     * @param str $key
     * @param mixed $value
     * @return void
     */
    static public function set($key, $value)
    {
        // check if parent exists...
        $NS  = SPRPC_Utility_Registry::get('__CONF_CURRENT_NS');
        $key = strtoupper($key);
        if (!empty($NS)) {
            $parentNS = SPRPC_Utility_Registry::get('__CONF_PARENT_NS');
            // try get parent conf...
            $parentConf = self::get($key, null, $parentNS);
            // merge if necessary...
            if (is_array($value) && is_array($parentConf)) {
                // not array_merge, but iteration to ensure no wrong error
                foreach ($value as $k => $v) {
                    // so this way it gets overriden completely...
                    $parentConf[$k] = $v;
                }
                $value = $parentConf;
            }

            // now just set it...
            $currentConf = SPRPC_Utility_Registry::get('__CONF_NS_' . $NS);
            if (is_array($currentConf)) {
                $currentConf[$key] = $value;
            } else {
                $currentConf = array($key => $value);
            }

            // set...
            SPRPC_Utility_Registry::register('__CONF_NS_' . $NS, $currentConf, true);

        } else {
            throw new Exception('CONFIGURATION ERROR: namespace is empty');
        }

    }// end set


    /**
     * get conf
     * @param str $env
     * @param mixed $default
     * @param str $key
     * @return mixed
     */
    static public function get($key = null, $default = null, $env = null)
    {
        // default env...
        if (empty($env)) {
            $env = self::getCurrentEnv();
        }

        $env  = strtoupper($env);
        $key  = strtoupper($key);
        $conf = SPRPC_Utility_Registry::get('__CONF_NS_' . $env);
        if (empty($key)) {
            // get all out for ns...
            return $conf;
        }

        // otherwise, get the key out..
        if (isset($conf[$key])) {
            return $conf[$key];
        }

        // otherwise, default
        return $default;

    }// end get

}// end SPRPC_Utility_Config
?>